// pages/listspage/listpage.js
var util = require('../../utils/util.js')
var dinnerlist;
const app = getApp();
var reDeleteItem = -1;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHidden:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  editList: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../foodspage/foodspage?id=' + id
    })
  },

  deleteList: function(event) {
    var id = event.currentTarget.dataset.id;
    console.log("长按:" + id);
    reDeleteItem = id;
    var message = "是否删除该列表？";
    this.setData({ message: message, isHidden: false});
  },

  changeSelected: function (event) {
    var id = event.currentTarget.dataset.id;
    dinnerlist.selected = id;
    util.setDinnerList(dinnerlist);
    this.setData({ dinnerlist: dinnerlist });
  },

  addNewList: function (event) {
    console.log("添加列表");
    wx.navigateTo({
      url: '../foodspage/foodspage?id=' + -1
    })
  },

  onCancelButtonClick: function(event) {
    console.log("onCancelButtonClick");
  },

  onConfirmButtonClick: function (event) {
    console.log("onConfirmButtonClick");
    if (reDeleteItem != -1) {
      dinnerlist.list.splice(reDeleteItem, 1);
      var list = dinnerlist.list;
      if(reDeleteItem == dinnerlist.selected) {
        dinnerlist.selected = 0;
      }
      reDeleteItem = -1;
      this.setData({ lists: list, dinnerlist: dinnerlist });
      console.log(dinnerlist);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dinnerlist = util.getDinnerList();
    var list = dinnerlist.list;
    console.log(list);
    this.setData({ lists: list, dinnerlist: dinnerlist });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    util.setDinnerList(dinnerlist);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})